package com.stormrage.edu.ctrl.edu;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.stormrage.edu.serv.edu.EduCourseServ;
import com.stormrage.edu.serv.edu.EduMajorServ;
import com.stormrage.edu.serv.edu.EduProjectServ;
import com.stormrage.edu.serv.learn.LeaTermServ;
import com.stormrage.edu.serv.student.StuAcctServ;
import com.stormrage.entity.edu.EduMajor;
import com.stormrage.entity.edu.EduProject;
import com.stormrage.entity.edu.rep.EduCourseRep;
import com.stormrage.entity.learn.LeaTerm;
import com.stormragetech.core.ctrl.BaseCtrl;

/**
 * @Title: EduMajorCtrl.java
 * @Package com.stormrage.edu.ctrl.edu
 * @Description: 专业 Controller
 * @author www.stormragetech.com
 * @date 2016年4月17日
 * @version V1.0
 */
@Controller
@RequestMapping("/major")
public class EduMajorCtrl extends BaseCtrl {

	private static final Logger logger = LoggerFactory.getLogger(EduMajorCtrl.class);

	@Value("#{constants['html.title']}")
	private String htmlTitle;

	@Value("#{constants['html.logo']}")
	private String htmlLogo;

	@Value("#{constants['url.root']}")
	private String rootUrl;

	@Value("#{constants['path.image']}")
	private String imagePath;

	@Autowired
	private EduProjectServ eduProjectServ;

	@Autowired
	private EduMajorServ eduMajorServ;

	@Autowired
	private EduCourseServ eduCourseServ;

	@Autowired
	private LeaTermServ leaTermServ;

	@Autowired
	private StuAcctServ stuAcctServ;

	@RequestMapping("/{pid}/{id}")
	public String major(@PathVariable("pid") Integer pid, @PathVariable("id") Integer id, Model model) {

		logger.debug("【 Controller 】-【 EduMajorCtrl 】-【 Major 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			if (currentUser.getPrincipal() != null)
				model.addAttribute("user", stuAcctServ.queryByAcct(currentUser.getPrincipal().toString()));

			// ------------------ 获取 Menu ------------------
			List<EduProject> menus = eduProjectServ.queryList();

			// ------------------ 获取 专业 学期 课程 ------------------
			EduMajor major = eduMajorServ.queryById(id);
			List<EduMajor> majorArray = eduMajorServ.queryListById(pid);
			LeaTerm term = leaTermServ.queryForMajor(id);
			List<EduCourseRep> course = eduCourseServ.queryRepListByMajorId(id);

			logger.debug(" course is " + course.toString());

			String path = rootUrl + imagePath;

			// ------------------ Model 赋值 ------------------
			model.addAttribute("menus", menus);
			model.addAttribute("major", major);
			model.addAttribute("array", majorArray);
			model.addAttribute("term", term);
			model.addAttribute("course", course);
			model.addAttribute("path", path);
			model.addAttribute("pid", pid);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 EduMajorCtrl 】-【 Major 】-【 GET 】-【 COMPLETED 】");
		return "edu/major";
	}

}
